using UnityEngine;
using System.Collections;

public class SoundManager : MonoBehaviour {

    public GameObject normalMusic;
    public GameObject bossMusic;
    public float baseVolume = .655f;
    public float fadeTime = 1.0f;
    float fadeProgress;
    bool isFading;

	// Use this for initialization
	void Start () {
        normalMusic.audio.volume = baseVolume;
        bossMusic.audio.volume = 0.0f;
	}
	
	// Update is called once per frame
	void Update () {
        if (!isFading)
        {
            GameManager man = gameObject.GetComponent<GameManager>();
            if (man.inFinalStage)
            {
                isFading = true;
                fadeProgress = 0.0f;
            }
        }
        if (isFading)
        {
            fadeProgress += Time.deltaTime;
            if (fadeProgress < fadeTime / 2.0f)
            {
                normalMusic.audio.volume = Mathf.Max(baseVolume * (1.0f - fadeProgress * 2.0f / fadeTime), 0.0f);
                bossMusic.audio.volume = 0.0f;
            }
            else
            {
                normalMusic.audio.volume = 0.0f;
                bossMusic.audio.volume = Mathf.Min(baseVolume * (fadeProgress - fadeTime/2.0f) * 2.0f / fadeTime, baseVolume);
            }
        }
	}
}
